package com.shiwaixiangcun.customer.module.heath;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.shiwaixiangcun.customer.GlobalApi;
import com.shiwaixiangcun.customer.GlobalConfig;
import com.shiwaixiangcun.customer.R;
import com.shiwaixiangcun.customer.base.BaseActivity;
import com.shiwaixiangcun.customer.entity.ToolBean;
import com.shiwaixiangcun.customer.ui.dialog.ShareDialog;
import com.shiwaixiangcun.customer.utils.AppSharePreferenceMgr;
import com.shiwaixiangcun.customer.utils.LoginHelper;
import com.shiwaixiangcun.customer.widget.ChangeLightImageView;
import com.shiwaixiangcun.customer.widget.X5WebView;
import com.tencent.smtt.sdk.CookieManager;
import com.tencent.smtt.sdk.CookieSyncManager;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 通用的Activity  处理评测内容
 *
 * @author Administrator
 */

public class CommonActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.back_left)
    ChangeLightImageView mBackLeft;
    @BindView(R.id.tv_page_name)
    TextView mTvPageName;
    @BindView(R.id.iv_share_right)
    ImageView mIvShareRight;
    @BindView(R.id.webview)
    X5WebView mWebView;

    private ToolBean mToolBean;
    private String type = "";
    private StringBuilder url = new StringBuilder();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_common);
        ButterKnife.bind(this);
        Bundle bundle = getIntent().getExtras();
        mToolBean = bundle.getParcelable("healthEvaluating");
        Log.e(BUG_TAG, mToolBean.name);
        initData();
        initWebView();
        initView();

    }

    /***
     * WebView配置
     */
    private void initWebView() {


    }

    private void initData() {
        switch (mToolBean.id) {
            case 1:
                type = "Chinese_Medicine";
                break;
            case 2:
                type = "Dietary_Dabits";
                break;
            case 3:
                type = "Endocrine_System";
                break;
            case 4:
                type = "Immune_System";
                break;
            case 5:
                type = "Digestive_System";
                break;
            case 6:
                type = "Respiratory_System";
                break;
            case 7:
                type = "Circulatory_System";
                break;
            case 8:
                type = "Sport_System";
                break;
            case 9:
                type = "Psychology_System";
                break;
            case 10:
                type = "Man_System";
                break;
            case 11:
                type = "Woman_System";
                break;

            default:
                break;
        }
    }

    private void initView() {
        url.append(GlobalApi.INSTANCE.getGetEvaluating()).append("?assessmentType=").append(type);
        String token = (String) AppSharePreferenceMgr.get(mContext, GlobalConfig.TOKEN, "");
        removeCookie(mContext);
        String userAgentString = mWebView.getSettings().getUserAgentString();
        //设置user_agent(以asyncHttprequest为例)
//        client.setUserAgent(defaultUserAgent);

        HashMap<String, String> header = new HashMap<String, String>();
        header.put("user-agent", userAgentString);
        mWebView.clearCache(true);
        if (LoginHelper.INSTANCE.isLogin(this)) {
            url.append("&access_token=").append(token);
        }
        mTvPageName.setText(mToolBean.name);
        mIvShareRight.setVisibility(View.VISIBLE);
        mBackLeft.setOnClickListener(this);
        mIvShareRight.setOnClickListener(this);
        Log.e(BUG_TAG, url.toString());
        mWebView.loadUrl(url.toString());
        mWebView.setWebViewClient(new MyWebViewClient());

    }


    /**
     * 清除Cookie
     *
     * @param context
     */
    private void removeCookie(Context context) {
        CookieSyncManager.createInstance(context);
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.removeAllCookie();
        CookieSyncManager.getInstance().sync();
    }


    @Override
    protected void onDestroy() {
        if (mWebView != null) {
            mWebView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
            mWebView.clearHistory();

            ((ViewGroup) mWebView.getParent()).removeView(mWebView);
            mWebView.destroy();
            mWebView = null;
        }
        super.onDestroy();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_left:
                finish();
                break;
            case R.id.iv_share_right:
                Toast.makeText(mContext, "分享", Toast.LENGTH_SHORT).show();


                showShare();
                break;
            default:
                break;
        }

    }

    private void showShare() {
        String imageUrl = "http://resource.hxteb.com/group1/M00/00/26/rBKx5Vl4TMCAUPgUAAB6YxNdWvs030.png";
        ShareDialog shareDialog = new ShareDialog();
        Bundle bundle = new Bundle();
        bundle.putString("title", mToolBean.name);
        bundle.putString("titleUrl", url.toString());
        bundle.putString("text", url.toString());
        bundle.putString("imageUrl", imageUrl);
        bundle.putString("siteUrl", url.toString());
        shareDialog.setArguments(bundle);
        shareDialog.show(this.getFragmentManager(), "shareDialog");



    }


    /**
     * 控制WebView加载
     */
    private final class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
}
